Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
gettext-parser
Advanced tools
Parse and compile gettext po and mo files to/from json, nothing more, nothing less
The gettext-parser npm package is a tool for parsing and compiling gettext files, which are commonly used for internationalization and localization of software. It supports both .po (Portable Object) and .mo (Machine Object) file formats.
Parsing .po files
This feature allows you to parse .po files into a JavaScript object. The code sample reads a .po file from the filesystem and parses it using gettext-parser.
const gettextParser = require('gettext-parser');
const fs = require('fs');
const input = fs.readFileSync('path/to/your/file.po');
const po = gettextParser.po.parse(input);
console.log(po);
Compiling .po files to .mo files
This feature allows you to compile .po files into .mo files. The code sample reads a .po file, parses it, compiles it into a .mo file, and writes the .mo file to the filesystem.
const gettextParser = require('gettext-parser');
const fs = require('fs');
const input = fs.readFileSync('path/to/your/file.po');
const po = gettextParser.po.parse(input);
const mo = gettextParser.mo.compile(po);
fs.writeFileSync('path/to/your/file.mo', mo);
Parsing .mo files
This feature allows you to parse .mo files into a JavaScript object. The code sample reads a .mo file from the filesystem and parses it using gettext-parser.
const gettextParser = require('gettext-parser');
const fs = require('fs');
const input = fs.readFileSync('path/to/your/file.mo');
const mo = gettextParser.mo.parse(input);
console.log(mo);
Compiling .mo files to .po files
This feature allows you to compile .mo files into .po files. The code sample reads a .mo file, parses it, compiles it into a .po file, and writes the .po file to the filesystem.
const gettextParser = require('gettext-parser');
const fs = require('fs');
const input = fs.readFileSync('path/to/your/file.mo');
const mo = gettextParser.mo.parse(input);
const po = gettextParser.po.compile(mo);
fs.writeFileSync('path/to/your/file.po', po);
node-gettext is a comprehensive library for handling gettext translations in Node.js. It provides functionalities for loading, parsing, and managing translations, similar to gettext-parser, but also includes additional features like translation context management and plural forms handling.
i18next is a powerful internationalization framework for JavaScript. While it does not focus solely on gettext files, it supports a wide range of translation formats and provides extensive features for managing translations, including interpolation, nesting, and pluralization. It is more feature-rich compared to gettext-parser but may be overkill if you only need to handle gettext files.
gettext.js is a lightweight library for handling gettext translations in JavaScript. It provides basic functionalities for parsing and compiling .po and .mo files, similar to gettext-parser, but with a simpler API and fewer features.
Parse and compile gettext po and mo files with node.js, nothing more, nothing less.
Include the library:
var gettextParser = require("gettext-parser");
Parse a PO file with
gettextParser.po.parse(input[, defaultCharset]) → Object
Where
"CHARSET"
(applies only if input is a Buffer)Method returns gettext-parser specific translation object (see below)
Example
var input = require('fs').readFileSync('en.po');
var po = gettextParser.po.parse(input);
console.log(po.translations['']); // output translations for the default context
PO files can also be parsed from a stream source. After all input is processed the parser emits a single 'data' event which contains the parsed translation object.
gettextParser.po.createParseStream([defaultCharset][, streamOptions]) → Transform Stream
Where
"CHARSET"
Example
var input = require('fs').createReadStream('en.po');
var po = gettextParser.po.createParseStream();
input.pipe(po);
po.on('data', function(data){
console.log(data.translations['']); // output translations for the default context
});
If you have a translation object you can convert this to a valid PO file with
gettextParser.po.compile(data[, options]) → Buffer
Where
Example
var data = {
...
};
var output = gettextParser.po.compile(data);
require('fs').writeFileSync(output);
Parse a MO file with
gettextParser.mo.parse(input[, defaultCharset]) → Object
Where
"CHARSET"
Method returns gettext-parser specific translation object (see below)
Example
var input = require('fs').readFileSync('en.mo');
var mo = gettextParser.mo.parse(input);
console.log(mo.translations['']); // output translations for the default context
If you have a translation object you can convert this to a valid MO file with
gettextParser.mo.compile(data) → Buffer
Where
Example
var data = {
...
};
var output = gettextParser.mo.compile(data);
require('fs').writeFileSync(output);
If you are compiling a previously parsed translation object, you can override the output charset with the charset
property (applies both for compiling mo and po files).
var obj = gettextParser.po.parse(inputBuf);
obj.charset = "windows-1257";
outputBuf = gettextParser.po.compile(obj);
Headers for the output are modified to match the updated charset.
By default gettext-parser uses pure JS iconv-lite for encoding and decoding non UTF-8 charsets. If you need to support more complex encodings that are not supported by iconv-lite, you need to add iconv as an additional dependency for your project (gettext-parser will detect if it is available and tries to use it instead of iconv-lite).
Parsed data is always in unicode but the original charset of the file can
be found from the charset
property. This value is also used when compiling translations
to a mo or po file.
Headers can be found from the headers
object, all keys are lowercase and the value for a key is a string. This value will also be used when compiling.
Translations can be found from the translations
object which in turn holds context objects for msgctxt
. Default context can be found from translations[""]
.
Context objects include all the translations, where msgid
value is the key. The value is an object with the following possible properties:
translator
, reference
, extracted
, flag
, previous
.Example
{
"charset": "iso-8859-1",
"headers": {
"content-type": "text/plain; charset=iso-8859-1",
"plural-forms": "nplurals=2; plural=(n!=1);"
},
"translations": {
"": {
"": {
"msgid": "",
"msgstr": ["Content-Type: text/plain; charset=iso-8859-1\n..."]
}
},
"another context": {
"%s example": {
"msgctxt": "another context",
"msgid": "%s example",
"msgid_plural": "%s examples",
"msgstr": ["% näide", "%s näidet"],
"comments": {
"translator": "This is regular comment",
"reference": "/path/to/file:123"
}
}
}
}
}
Notice that the structure has both a headers
object and a ""
translation with the header string. When compiling the structure to a mo or a po file, the headers
object is used to define the header. Header string in the ""
translation is just for reference (includes the original unmodified data) but will not be used when compiling. So if you need to add or alter header values, use only the headers
object.
If you need to convert gettext-parser formatted translation object to something else, eg. for jed, check out po2json.
MIT
FAQs
Parse and compile gettext po and mo files to/from json, nothing more, nothing less
We found that gettext-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.